package Game;

//import java.awt.*;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

public class InputManager implements KeyListener, MouseListener, MouseMotionListener
{
	static int keyCode;
	static char keyChar;
	static int posX;
	static int posY;
	static String texto;
	
	static ArrayList<Integer> ListkeyCode = new ArrayList<Integer>();;
	static ArrayList<Integer> ListkeyCode_Relesead = new ArrayList<Integer>();;
	

	public InputManager()
	{
		InputManager.ListkeyCode.add(0);
		InputManager.ListkeyCode_Relesead.add(0);
	}
	
	public void Update()
	{
			
		for(int i = 0; i < ListkeyCode.size(); i++)
		{			
			for(int x = 0; x < ListkeyCode_Relesead.size(); x++)
			{
				if(ListkeyCode.get(i) == ListkeyCode_Relesead.get(x))
				{
					if(ListkeyCode.get(i) != 0)
					{
						ListkeyCode.remove(ListkeyCode.get(i));
						ListkeyCode_Relesead.remove(ListkeyCode_Relesead.get(x));
					}
				}
			}
		}
	}
	
	public boolean ChecarTecla(int x)
	{
		for(Integer lkc : ListkeyCode)
		{
			if(x == lkc)
				return true;
		}
		
		return false;
	}
	

	public void keyPressed(KeyEvent e){
		
		keyCode = e.getKeyCode();
		boolean repetido = false;
		for(Integer lkc : ListkeyCode)
		{
			if(lkc != keyCode)
				repetido = false;
			else
			{
				repetido = true;
				break;
			}
		}
		if(repetido == false)
		ListkeyCode.add(keyCode);
		
		
		keyChar = ' ';
		//Arkanoid.texto = "CARALHOU";
		//repaint();
	}
	public void keyReleased(KeyEvent e)	
	{
		keyCode = e.getKeyCode();
		boolean repetido = false;
		for(Integer lkc : ListkeyCode_Relesead)
		{
			if(lkc != keyCode)
				repetido = false;
			else
			{
				repetido = true;
				break;
			}
		}
		if(repetido == false)
		ListkeyCode_Relesead.add(keyCode);
		keyChar = ' ';
	}
	public void keyTyped(KeyEvent e){
		keyChar = e.getKeyChar();
		//repaint();
	}
	
	
	
	
	public void mousePressed(MouseEvent e) 	{
       mudaTexto("Mouse clicando; cliques: " + e.getClickCount());
	}
	public void mouseReleased(MouseEvent e)	{
		mudaTexto("Mouse solto; cliques: " + e.getClickCount());
		posX = e.getX();
		posY = e.getY();
	}
	public void mouseEntered(MouseEvent e) {
		mudaTexto("Mouse na tela");
		
	}
	public void mouseExited(MouseEvent e) {
		mudaTexto("Mouse fora da tela");
	}
	public void mouseClicked(MouseEvent e) {
		mudaTexto("Mouse clicado (cliques: " + e.getClickCount() + ")");
	}
	
	
	
	public void mouseMoved(MouseEvent e) {
		posX = e.getX();
		posY = e.getY();
		
	}
	


	
	void mudaTexto(String texto2) {
	    texto = texto2; // + " detected on " + e.getComponent().getClass().getName() + ".");
	    
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void Debug(Graphics g2d)
	{
		/// DEBUG DO TECLADO
		g2d.drawString("Press a key...", 20, 20);
		g2d.drawString("Key code: "  + InputManager.keyCode, 600, 30);
		g2d.drawString("Key char: "  + InputManager.keyChar, 600, 50);
		g2d.drawString("Apertadas: " + InputManager.ListkeyCode, 600, 70);
		g2d.drawString("Soltas: "    + InputManager.ListkeyCode_Relesead, 600, 90); 
		g2d.drawString("Estado do Mouse:" + InputManager.texto, 600, 120);
		g2d.drawString("Posicao Mouse:" + InputManager.posX + " - " + InputManager.posY, 600, 140);
	}

	
	
}
